package com.tbs.parkingsaleapp.UI.LoginOrSign.Base;

import abstergo.tbs.betterasyc.Handler.HandlerMananger;
import android.os.Bundle;
import android.os.Message;
import android.widget.*;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.tbs.parkingsaleapp.Http.IAsycHttpResultAction;
import com.tbs.parkingsaleapp.Http.JsonData.FaceCheckResult;
import com.tbs.parkingsaleapp.SystemManager.LoadToastMananger;
import com.tbs.parkingsaleapp.UI.Listenners.SignClickListenner;
import com.tbs.parkingsaleapp.UI.Listenners.TakeFaceStrOnClickListenner;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.SystemManager.Handler.CallBacks.SignFaceExireHandler;
import com.tbs.parkingsaleapp.SystemManager.Handler.HandlerConst;
import com.tbs.parkingsaleapp.UI.LoginOrSign.LoginOrSign;

public class SignFragment extends Fragment implements IAsycHttpResultAction<FaceCheckResult> {

     FaceCheckResult result;
     boolean faceisok=false;

    public FaceCheckResult getResult() {
        return result;
    }


    public boolean isFaceisok() {
        return faceisok;
    }

    public void setFaceisok(boolean faceisok) {
        this.faceisok = faceisok;
        if(faceisok)
        {
            checkview.setVisibility(View.VISIBLE);

        }
        else
            checkview.setVisibility(View.INVISIBLE);
    }

    public SignFragment() {

    }
    //注册人脸识别结果
    @Override
    public void ResultFunction(FaceCheckResult result) {
        if(result!=null)
        {

            this.result=result;
            LoadToastMananger.Hide();
            if(result.getResultCode()==1)
            {
                if(result.getSigned()==1)
                {
                    Toast.makeText(getContext(),"您已注册过，请直接登录",Toast.LENGTH_SHORT).show();
                    return;
                }
                Message ms=new Message();
                ms.what= HandlerConst.FaceExpire;
                HandlerMananger.Instance().Subscribe(HandlerConst.FaceExpire,new SignFaceExireHandler(this),true);
                HandlerMananger.Instance().SendMsg(ms,(long)(result.getToken().getRemaining()*1000));
                setFaceisok(true);
                Toast.makeText(getContext(), String.format("本次人脸验证已通过，有效时间剩余%.2f秒",result.getToken().getRemaining()), Toast.LENGTH_SHORT).show();
                id.setText(result.getFaceID());
            }
            else
            {
                setFaceisok(false);
                Toast.makeText(getContext(), "本次人脸验证失败，请尝试重新验证", Toast.LENGTH_SHORT).show();
            }
        }
        else
        {
            Toast.makeText(getContext(),"请检查网络",Toast.LENGTH_SHORT).show();
        }

    }
    public class UserData
    {
        public String name,id,password,phone;
        public  String face="1111111";
    }

    public  UserData Collect()
    {
        UserData data=new UserData();
        if(result!=null)
        data.face=result.getToken().getToken();
        data.id=id.getText().toString();
        data.name=username.getText().toString();
        data.password=password.getText().toString();
        data.phone=phone.getText().toString();
        return data;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    ImageView faceview,checkview;
    EditText username,id,password,phone;
    TextView sign;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View main=  inflater.inflate(R.layout.fragment_sign, container, false);
        faceview= main.findViewById(R.id.face);
        faceview.setOnClickListener(new TakeFaceStrOnClickListenner(getActivity(), LoginOrSign.R_Sign));
        username=main.findViewById(R.id.input_username);
        id=main.findViewById(R.id.input_id);
        password=main.findViewById(R.id.input_password);
        phone=main.findViewById(R.id.input_phone);
        sign=main.findViewById(R.id.btm_signin);
        sign.setOnClickListener(new SignClickListenner(this));
        checkview= main.findViewById(R.id.checkImage);
        return main;
    }
}